#!/usr/bin/python
import unittest
import sys
import os

from StringIO import StringIO

from uya.routerconf.configmod.configobj import configobj
from uya.routerconf.configmod.configobj import validate
from uya.routerconf import validators
import cherrypy
from cherrypy._cpwsgi import wsgiApp
from cherrypy.test import helper

# Import app modules
from uya.routerconf import misc
from uya.routerconf.configmod import config as configmod

from uya.configmod_web import configuration 

configcontents = """
    [section1]  
        parameter1 = 1
        parameter2 = hello
        [[section1a]]
            x = 1a
        [[section1b]]
            y = 1b
"""

templatecontents = """
    [section1] 
        parameter1 = integer(default=5)
        parameter2 = string(max=30)
        [[section1a]]
            x = string
        [[section1b]]
            y = string
"""

viewcontents = """
    [section1] 
        parameter1 = view(help="help text", fields="2|(\d+).(\d+)|%s.%s", default=None)
"""

callbacks = {}
authlst = [("user", "password"), ("user2", "password2")]

expected_sections =  [
    [
        ('section1', None, None),
    ], 
    [
        ('section1a', '/admin/section1/section1a', None), 
        ('section1b', '/admin/section1/section1b', None),
    ]
]

expected_parameters = [
    (["section1"], 'parameter1', 1, 
        misc.Struct(type="integer", default="5", 
            view={"help": "help text", "fields": "2|(\d+).(\d+)|%s.%s"}, 
            args=[], kwargs={})), 
    (["section1"], 'parameter2', 'hello', 
        misc.Struct(type="string", default=None, view={}, args=[], 
            kwargs={"max": "30"})), 
]

class TestConfiguration(helper.CPWebCase):        

    def setUp(self):
        self.validator = validate.Validator()
        self.config = configuration.load_config(StringIO(configcontents),
            StringIO(templatecontents), StringIO(viewcontents), self.validator)
                        
    def test_build_path(self):
        url = configuration.build_path("admin", [])
        self.assertEqual("/admin", url) 
        url = configuration.build_path("admin2", ["a", "b", "c"])
        self.assertEqual("/admin2/a/b/c", url) 

    def test_get_config(self):
        config = self.config
        subconfig = configuration.get_config_path(config, ["section1"])
        self.assertEqual(config["section1"].keys(), subconfig.keys())
        subconfig = configuration.get_config_path(config, ["section1", "section1a"])
        self.assertEqual("1a", subconfig["x"])         

    def test_build_urls(self):                
        sections, parameters0, showinfo = \
            configuration.build_urls(self.config, self.validator, "admin", ["section1"])
        self.assertEqual(expected_sections, sections)
        groupname, parameters = parameters0[0]
        self.assertEqual(expected_parameters, parameters)

    def test_get_validate_info(self):
        path = ["section1"]
        info = configuration.get_validate_info(self.config, path, "parameter1")
        expected_info = misc.Struct(
            default='5', 
            view={'fields': '2|(\\d+).(\\d+)|%s.%s', 'help': 'help text'}, 
            type='integer', 
            args=[], 
            kwargs={})
        self.assertEqual(expected_info, info)

    def XXXtest_group_parameters(self):
        path = ["section1"]
        configspec = configuration.get_config_path(self.config, path).configspec
        params = {"parameter1#0": "1", "parameter1#1": "2", "parameter2": "bye"}
        parameters = list(configuration.group_parameters(params, self.validator, configspec))
        self.assertEqual({"parameter1": "1.2", "parameter2": "bye"}, dict(parameters))
        
def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestConfiguration)
                                            
if __name__ == '__main__':
    unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
